package com.web.notice.action;

import java.io.BufferedReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.util.BaseActionUtil;
import com.util.JsonUtil;
import com.web.notice.po.NoticePo;
import com.web.notice.service.NoticeService;
import com.web.system.po.UserPo;
import com.web.system.service.UserService;

@Controller @Scope("prototype") 
@ParentPackage(value = "struts-default")
@Namespace(value = "/")
@Action(value = "noticeAction",results={
		@Result(name="first_page", location="/jsp/notice/first_page_noticelist.jsp"),
		@Result(name="modify", location="/jsp/notice/modify_notice.jsp"),
		@Result(name="look", location="/jsp/notice/look_one_notice.jsp"),
		@Result(name="delete_success",type="chain", location="noticeAction")
})
public class NoticeAction extends BaseActionUtil implements ModelDriven<NoticePo>{

	private static final long serialVersionUID = 22025029333865843L;
	
	@Resource
	private NoticeService noticeService;

	@Resource
	private UserService userService;
	
	private NoticePo noticePo = null;

	public NoticePo getModel() {
		// TODO Auto-generated method stub
		noticePo = new NoticePo();
		return noticePo;
	}
    
	public String firstPageNoticeList() throws Exception {
		
		String noticeId = request.getParameter("noticeId");
		NoticePo noticePo = null;
		List<NoticePo> noticePos = noticeService.getFirstPageNoticeList();
		if(!"".equals(noticeId) && !"null".equalsIgnoreCase(noticeId) && noticeId != null){
			noticePo = noticeService.getNoticeByNoticeId(new Integer(noticeId));
		} else {
			if(null != noticePos && noticePos.size() > 0){
				noticePo = noticePos.get(0);
			}
		}
		request.setAttribute("noticePos", noticePos);
		request.setAttribute("noticePo", noticePo);
		return "first_page";
	}
	
	/**
	 * 审核公告通过/不通过
	 * @return
	 * @throws Exception
	 */
	public String checkNotice() throws Exception {
		String showState = request.getParameter("showState");
		String ids = request.getParameter("ids");
		//UserPo userPo = (UserPo) session.getAttribute("user");
		String sql = "update NoticePo set showState='"+showState+"' where noticeId in ("+ids+")";
		//if("2".equals(showState)){//审核通过
		//	List<UserPo> userPos = userService.getAllUsers("exceptMe", userPo);
			out.print(noticeService.executeBySql(sql,ids,showState));
		//} else {
		//	out.print(noticeService.executeBySql(sql,userPo));
		//}
		out.flush();
		out.close();
		return NONE;
	}
	
	
	/**
	 * 添加修改公告
	 * @return
	 * @throws Exception
	 */
	public String saveOrUpdateNotice() throws Exception {
		
		UserPo userPo = (UserPo) session.getAttribute("user");
		String modify = request.getParameter("modify");
		int save = 0;
		if("1".equals(modify)){
			save = 1;
		}
        //公告内容
		String noticeContext = noticePo.getNoticeContent();
		Reader reader = new StringReader(noticeContext);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = null;
		StringBuffer strBuffer = new StringBuffer();
		while ((line = bufferedReader.readLine()) != null) {
			strBuffer.append(line);
			strBuffer.append("\n");
		}
	
		noticePo.setNoticeContent(strBuffer.toString());
		if(!"1".equals(modify)){
			noticePo.setNoticeAddTime(new Date());
			noticePo.setNoticeAddUser(userPo.getUserName());
			noticePo.setShowState(0);
		}
		/**
		 * 3：将数据与文件路径保存到表中。
		 */
		
		if(noticeService.saveOrUpdateNotice(noticePo, save , userPo)){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("notice", noticePo);
			out.print(noticePo.getNoticeId());
			out.flush();
			out.close();
		}
		return NONE;
	}
	
	
	
	public String getNoticeByNoticeId() throws Exception {
		String look = request.getParameter("look");
		String noticeId = request.getParameter("noticeId");
		noticePo = noticeService.getNoticeByNoticeId(new Integer(noticeId));
		
		request.setAttribute("noticePo", noticePo);
		//单独查看
		if("one".equals(look)){
			return "look";
		} else if("modify".equals(look)){
			return "modify";
		}
		
		return NONE;
	}
	
	public String deleteNotice() throws Exception {
		String ids = request.getParameter("ids");
		out.print(noticeService.deleteNoticesByNoticeIds(ids));
		out.flush();
		out.close();
		return NONE;
	}
	
	
	public String getNoticeList() throws Exception {
		
		UserPo userPo = (UserPo) session.getAttribute("user");
		String isMyNoticeList = request.getParameter("isMyNoticeList");
		
		//分页部分
		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("rows"));
		int first = pageSize * (page - 1);
		
		//搜索部分
		String sql = "";
		//my的列表
		if(isMyNoticeList != null && !"".equals(isMyNoticeList) && !"null".equals(isMyNoticeList)){
			sql += " and notice.noticeAddUser = '" + userPo.getUserName() + "'";
		}
		String noticeIdSearch = request.getParameter("noticeIdSearch");
		String noticeTitleSearch = request.getParameter("noticeTitleSearch");
		String showStateSearch = request.getParameter("showStateSearch");
		String date_from = request.getParameter("date_from");
		String date_to = request.getParameter("date_to");
		
		if(showStateSearch != null && !"".equals(showStateSearch) && !"null".equals(showStateSearch)){
			sql += " and notice.showState = '" + showStateSearch + "'";
		}
		if(noticeIdSearch != null && !"".equals(noticeIdSearch) && !"null".equals(noticeIdSearch)){
			sql += " and notice.noticeId = '" + noticeIdSearch + "'";
		}
		if(noticeTitleSearch != null && !"".equals(noticeTitleSearch) && !"null".equals(noticeTitleSearch)){
			sql += " and notice.noticeTitle like '%"+noticeTitleSearch+"%'";
		}
		if(date_from != null && !"".equals(date_from) && !"null".equals(date_from)){
			sql += " and notice.noticeOverdueTime >= '" + date_from + "'";
		}
		if(date_to != null && !"".equals(date_to) && !"null".equals(date_to)){
			sql +=" and notice.noticeOverdueTime <= '" + date_to + "'";
		}
		
		//组合最终sql语句
		StringBuffer queryStringBuffer = new StringBuffer();
		queryStringBuffer.append("FROM NoticePo as notice");
		queryStringBuffer.append(" where 1=1 ");
		queryStringBuffer.append(sql);
		queryStringBuffer.append(" ORDER BY showState , noticeAddTime desc ");
		
        List<NoticePo> notices = noticeService.getNoticeList(queryStringBuffer.toString(), first, pageSize);
        
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        int total = noticeService.getTotal(sql);
        jsonMap.put("total", total);
        jsonMap.put("rows", notices);
        out.print(JsonUtil.objectToStr(jsonMap,new String[]{"documentPos"}));
        out.flush();
        out.close();
		return NONE;
	}
	

	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
